import { defineStore } from 'pinia'

export const useKeepAliveStore = defineStore('keepAlive', {
	state: () => ({
		keepLiveRoute: [],
		routeKey: null,
		routeShow: true
	}),
	
	getters: {
		// 获取缓存的路由组件列表
		getKeepLiveRoute: (state) => state.keepLiveRoute,
		
		// 获取路由键
		getRouteKey: (state) => state.routeKey,
		
		// 获取路由显示状态
		getRouteShow: (state) => state.routeShow,
		
		// 检查组件是否已缓存
		isComponentCached: (state) => (component) => {
			return state.keepLiveRoute.includes(component)
		}
	},
	
	actions: {
		// 添加需要缓存的组件
		pushKeepLive(component) {
			if (!this.keepLiveRoute.includes(component)) {
				this.keepLiveRoute.push(component)
			}
		},
		
		// 移除缓存的组件
		removeKeepLive(component) {
			const index = this.keepLiveRoute.indexOf(component)
			if (index !== -1) {
				this.keepLiveRoute.splice(index, 1)
			}
		},
		
		// 清空所有缓存
		clearKeepLive() {
			this.keepLiveRoute = []
		},
		
		// 设置路由键
		setRouteKey(key) {
			this.routeKey = key
		},
		
		// 设置路由显示状态
		setRouteShow(show) {
			this.routeShow = show
		}
	}
})